package common.com.androidbase.okhttp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.android.sohu.sdk.common.toolbox.LogUtils;
import com.common.sdk.net.connect.http.OkhttpManager;
import com.common.sdk.net.connect.http.model.HttpError;
import com.common.sdk.net.connect.http.model.OkHttpSession;
import com.common.sdk.net.connect.http.util.SohuRequestBuilder;
import com.common.sdk.net.connect.interfaces.IResponseListener;
import com.common.sdk.net.connect.interfaces.IResultParser;

import java.util.HashMap;
import java.util.Map;

import common.com.androidbase.R;
import common.com.androidbase.datarequest.DataRequestUtil;
import common.com.androidbase.datarequest.VideoInfoDataModel;
import okhttp3.Request;

/**
 * Created by chengjiangsang116630 on 2018/1/24.
 */

public class OkHttpTestActivity extends FragmentActivity {

    private static final String TAG = "OKHttpTestActivity";
    public static final String API_KEY = "9854b2afa779e1a6bff1962447a09dbd";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_okhttp_test);

        LogUtils.setDebugMode(true);

        Map<String, Object> baseParams = new HashMap<String, Object>();
        baseParams.put("api_key", API_KEY);
        baseParams.put("plat", "6");
        baseParams.put("sver", "5.9.0");
        baseParams.put("poid", "1");
        baseParams.put("partner", "680");
        baseParams.put("sysver", "5.1");

        SohuRequestBuilder.init(baseParams);

//        LogUtils.d(TAG, "OkHttpTestActivity onCreate");

        Log.d(TAG, "OKhttpTestActivity onCreate");

        OkhttpManager.init(this);

        Button button = (Button) findViewById(R.id.btn_async_test);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Request request = DataRequestUtil.getVideoDetailRequest(9035582, 2528347, 1);

                IResultParser parser = new OkhttpResultParser(VideoInfoDataModel.class);

                new OkhttpManager().enqueue(request, new IResponseListener() {
                    @Override
                    public void onSuccess(Object notNullData, OkHttpSession session) {
                        VideoInfoDataModel videoInfoDataModel = (VideoInfoDataModel) notNullData;
                        LogUtils.d(TAG, videoInfoDataModel.getStatusText());
                    }

                    @Override
                    public void onFailure(HttpError error, OkHttpSession session) {
                        LogUtils.e(TAG, error.getDesc(), error.getE());
                    }

                    @Override
                    public void onCancelled(OkHttpSession session) {
                    }
                }, parser);
            }
        });

        Button syncButton = (Button) findViewById(R.id.btn_sync_test);
        syncButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Request request = DataRequestUtil.getVideoDetailRequest(9035582, 2528347, 1);

                        IResultParser parser = new OkhttpResultParser(VideoInfoDataModel.class);

                        String json = new OkhttpManager().execute(request);
                        try {
                            Object obj = parser.parse(null, json);
                            VideoInfoDataModel videoInfoDataModel = (VideoInfoDataModel) obj;
                            LogUtils.d(TAG, videoInfoDataModel.getStatusText());
                        } catch (Exception e) {
                            LogUtils.e(TAG, e);
                        }
                    }
                }).start();


            }
        });

        Button formalTestButton = (Button) findViewById(R.id.btn_formal_2_test);
        formalTestButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String formalAddr = "http://api.tv.sohu.com";
                String testAddr = "http://testapi.hd.sohu.com";
                OkhttpManager.putDomainKeyValue("key", formalAddr, testAddr);
            }
        });

        Button testFormalButton = (Button) findViewById(R.id.btn_test_2_formal);
        testFormalButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String formalAddr = "http://api.tv.sohu.com";
                OkhttpManager.removeDomainKeyValue(formalAddr);
            }
        });

    }

}
